package com.hns.can.service.web;

import com.hns.can.base.entity.SysCorpInfoEntity;
import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.base.service.SysCorpInfoService;
import com.hns.can.common.domain.GridPage;
import com.hns.can.common.web.BaseController;
import com.hns.can.service.entity.BaseElectFencCarRelEntity;
import com.hns.can.service.entity.BaseElectFencEntity;
import com.hns.can.service.service.ElectFencSetService;
import com.hns.can.service.vo.ElectFencSetParamsVo;
import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.frame.mybatis.Pagination;
import com.hns.tool.export.ExportUtil;
import com.hns.tool.export.excel.excelNoTemplate.ExportExcelNoTempEntity;
import com.hns.tool.export.pdf.ExportPdfEntity;
import com.hns.tool.pub.PubUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 电子围栏
 * @author:Fisher
 * @email:zhuangcaijin@126.com
 * @version Revision 2.0.0
 */
@Controller
@RequestMapping("/electFencSet")
public class ElectFencSetController extends BaseController{

    @Autowired
    private ElectFencSetService electFencSetService;
    @Autowired
    private SysCorpInfoService sysCorpInfoService;
    
    final private String[] headerNames = new String[]{"编号","名称","机构名称","形状","功能"};
    final private String[] headerFields = new String[]{"fencCd", "fencName", "organName", "fencType","funcType"};

    /**
     * 根据Id获取电子围栏
     * @param fencId
     * @return
     * @throws Exception
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    @RequestMapping("/getBaseElectFencById")
    @ResponseBody
    public Object getBaseElectFencById(@RequestParam(value="fencId") String fencId) throws Exception {
        BaseElectFencEntity entity = (BaseElectFencEntity)electFencSetService.getBaseElectFencById(fencId);
        return this.getReturnResult(entity);
    }
    
    /**
     * 添加电子围栏
     * @param entity
     * @return
     * @throws Exception
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    @RequestMapping("/saveBaseElectFenc")
    @ResponseBody
    public Object saveBaseElectFenc(BaseElectFencEntity entity) throws Exception {
        //@tip(用于区别黑点围栏和电子围栏的不同)
        //黑点围栏：取公司所在的省份和城市信息
        //电子围栏：取组织所在的省份和城市信息
        if (PubUtil.isEmpty(entity.getProvinceId()) || PubUtil.isEmpty(entity.getCityId())) {
            SysUserEntity user = getCurrentUser().getUser();
            String corpId = user.getCorpId();
            SysCorpInfoEntity corpInfo = (SysCorpInfoEntity) sysCorpInfoService.searchSysCorpInfoByCorpId(corpId);

            entity.setProvinceId(corpInfo.getProvinceId());
            entity.setCityId(corpInfo.getCityId());
        }
        return electFencSetService.saveElectFenc(entity);
    }
    
    /**
     * 更新电子围栏
     * @param entity
     * @return
     * @throws Exception
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    @RequestMapping("/updateBaseElectFenc")
    @ResponseBody
    public Object updateBaseElectFenc(BaseElectFencEntity entity) throws Exception {
        electFencSetService.updateBaseElectFenc(entity);
        return this.getReturnMsg("修改成功");
    }
    
    /**
     * 根据ID删除电子围栏
     * @param fencId
     * @return
     * @throws Exception
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    @RequestMapping("/deleteBaseElectFencById")
    @ResponseBody
    public Object deleteBaseElectFencById(@RequestParam(value="fencId") String fencId) throws Exception {
        electFencSetService.deleteBaseElectFenc(fencId);
        return this.getReturnMsg("删除成功");
        
    }

    /**
     * 电子围栏查询
     * @param entity
     * @throws Exception
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    @RequestMapping("/searchElectFenc")
    @ResponseBody
    public Object searchElectFenc(BaseElectFencEntity entity) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
        entity.setCorpId(user.getCorpId());
        Pagination page = electFencSetService.searchElectFenc(entity);
        GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
    }

    /**
     * 绑定电子围栏
     * @param vo
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/setElectFenc")
    @ResponseBody
    public Object setElectFenc(ElectFencSetParamsVo vo) throws Exception {
        electFencSetService.setElectFenc(vo);
        return this.getReturnResult();
    }

    /**
     * 解绑电子围栏
     * @param vo
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/unsetElectFenc")
    @ResponseBody
    public Object unsetElectFenc(ElectFencSetParamsVo vo) throws Exception {
        electFencSetService.unsetElectFenc(vo);
        return this.getReturnMsg("解绑成功");
    }

    /**
     * 分页查询电子围栏与车辆关系
     * @param vo
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/searchElectFencCarRel")
    @ResponseBody
    public Object searchElectFencCarRel(ElectFencSetParamsVo vo) throws Exception {
        Pagination page = electFencSetService.searchElectFencCarRel(vo);
        GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
    }

    /**
     * 根据车辆ID查询电子围栏与车辆关系
     * @param carId
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/searchElectFencCarRelByCarId")
    @ResponseBody
    public Object searchElectFencCarRelByCarId(String carId) throws Exception {
        List<BaseElectFencCarRelEntity> list = electFencSetService.searchElectFencCarRelByCarId(carId);
        return this.getReturnResult(list);
    }
    
    /**
     * 电子围栏导出excel                
     * @param entity
     * @param response
     * @throws Exception
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    @RequestMapping("/exportElectFencExcel")
    public void exportElectFencExcel(BaseElectFencEntity entity, HttpServletResponse response) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
        entity.setCorpId(user.getCorpId());
        List list = electFencSetService.exportElectFenc(entity);
        if(PubUtil.isEmpty(list)){
            throw new BusinessException(MsgType.WARN.getFlag(), "没有数据导出！");
        }
        ExportExcelNoTempEntity exportEntity = new ExportExcelNoTempEntity(
            Arrays.asList(headerNames), Arrays.asList(headerFields), list, "electFenc");
        ExportUtil.generatorExcelNoTemplate(exportEntity, response);
    }
    
    /**
     * 电子围栏导出PDF
     * @param vo
     * @param response
     * @throws Exception
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    @RequestMapping("/exportElectFencPdf")
    public void exportElectFencPdf(BaseElectFencEntity vo, HttpServletResponse response) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
        vo.setCorpId(user.getCorpId());
        List list = electFencSetService.exportElectFenc(vo);
        if(PubUtil.isEmpty(list)){
            throw new BusinessException(MsgType.WARN.getFlag(), "没有数据导出！");
        }
        ExportPdfEntity pdfEntity = new ExportPdfEntity(
            Arrays.asList(headerNames), Arrays.asList(headerFields), list, "electFenc");
        
        ExportUtil.generatorPDF(pdfEntity, response);
    }
    
}
